package com.suredy.app.porject.ctrl.view;

import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.suredy.app.porject.constants.ContractStatus;
import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.model.Contract;
import com.suredy.app.porject.model.FlowOfContract2User;
import com.suredy.security.model.User;
import com.suredy.security.model.User2Role;

/**
 * 合同视图控制类
 * 
 * @author VIVID.G
 * @since 2015-12-25
 * @version v0.1
 */
public class ViewRoleCtrlForContract extends ViewRoleCtrl {

	private Contract contract;

	public ViewRoleCtrlForContract(Contract contract, User user, List<User2Role> roles) {
		super(roles, user);
		this.contract = contract;
	}

	public boolean isCreator() {
		if (this.contract == null || this.contract.getFlowInfo() == null)
			return false;

		if (this.user == null || StringUtils.isBlank(this.user.getId()))
			return false;

		for (FlowOfContract2User flow : this.contract.getFlowInfo()) {
			if (flow == null || flow.getStatus() == null || flow.getUser() == null)
				continue;

			if (StringUtils.isBlank(flow.getUser().getId()))
				continue;

			// 必须处于新建环节
			if (!FlowStatus.FS2000_CONTRACT_TO_CREATE.equals(flow.getStatus()))
				continue;

			if (flow.getUser().getId().equals(this.user.getId()))
				return true;
		}

		return false;
	}

	public boolean isThisXiangmuManager() {
		if (this.contract == null || this.contract.getXiangmuHeader() == null)
			return false;

		if (this.contract.getXiangmuHeader().getId() == null)
			return false;

		if (this.user == null)
			return false;

		return this.contract.getXiangmuHeader().getId().equals(this.user.getId());
	}

	public boolean isCanSend() {
		if (!this.isCreator() || !this.isContractSigning())
			return false;

		// 合同进行中随时可以发送
		return true;
	}

	public boolean isCanEditForm() {
		if (!this.isCreator() || !this.isContractSigning())
			return false;

		// 合同进行中随时可以编辑
		return true;
	}

	public boolean isCanStartContract() {
		if (!this.isStartContract())
			return false;

		if (!this.isContractStatusIn(ContractStatus.WAIT))
			return false;

		// 合同签订完成
		return FlowStatus.FS2001_CONTRACT_CREATED.equals(this.contract.getFlowStatus());
	}

	public boolean isCanRestartContract() {
		if (!this.isThisXiangmuManager())
			return false;

		return this.isContractStatusIn(ContractStatus.SUSPEND);
	}

	public boolean isCanPlanMoneyIn() {
		if (!this.isThisXiangmuManager())
			return false;

		// 在建、竣工都允许制定计划
		return this.isContractStatusIn(ContractStatus.START, ContractStatus.FINISH);
	}

	public boolean isCanPlanMoneyOut() {
		if (!this.isThisXiangmuManager())
			return false;

		// 在建、竣工都允许制定计划
		return this.isContractStatusIn(ContractStatus.START, ContractStatus.FINISH);
	}

	public boolean isCanPlanWork() {
		if (!this.isThisXiangmuManager())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanCreateTask() {
		if (!this.isCreateTaskContract())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanCreateMaterial() {
		if (!this.isCreateMaterialContract())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanCreateService() {
		if (!this.isCreateServiceContract())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanCreateProfessional() {
		if (!this.isCreateProfessionalContract())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanCreateAgreement() {
		if (!this.isCreateAgreementContract())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanFinishContract() {
		if (!this.isThisXiangmuManager())
			return false;

		if (this.contract == null)
			return false;

		// 子合同未完工
		if (!this.contract.isSubContractCompleted())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanPayout() {
		if (!this.isThisXiangmuManager())
			return false;

		return this.isContractStatusIn(ContractStatus.START, ContractStatus.FINISH);
	}

	public boolean isCanClearContract() {
		if (!this.isJijingHeader())
			return false;

		return this.isContractStatusIn(ContractStatus.FINISH);
	}

	public boolean isCanResetProjectTime() {
		if (!this.isThisXiangmuManager())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanSuspendContract() {
		if (!this.isThisXiangmuManager())
			return false;

		return this.isContractStatusIn(ContractStatus.START);
	}

	public boolean isCanShutdownContract() {
		if (!this.isThisXiangmuManager())
			return false;

		return this.isContractStatusIn(ContractStatus.START, ContractStatus.SUSPEND);
	}

	public boolean isCanUploadContractFile() {
		if (!this.isCreator() || !this.isContractSigning())
			return false;

		return this.contract.getContractFile() == null;
	}

	public boolean isCanUploadJungongFile() {
		// 必须为竣工才可以上传文件
		if (!this.isContractStatusIn(ContractStatus.FINISH))
			return false;

		// 必须为项目负责人
		if (!this.isThisXiangmuManager())
			return false;

		return this.contract.getJgFile() == null;
	}

	public boolean isCanUploadOtherFile() {
		if (!this.isCreator() || !this.isContractSigning())
			return false;

		return this.contract.getOtherFiles() == null;
	}

	public boolean isCanRemoveFile() {
		if (!this.isCreator() || !this.isContractSigning())
			return false;

		// 合同签订中
		return true;
	}

	private boolean isContractSigning() {
		return this.isUserActiveInFlow(FlowStatus.FS2000_CONTRACT_TO_CREATE);
	}

	private boolean isContractStatusIn(ContractStatus... status) {
		if (this.contract == null || status == null || status.length <= 0)
			return false;

		return ArrayUtils.contains(status, this.contract.getStatus());
	}

	private boolean isUserActiveInFlow(FlowStatus... status) {
		if (this.contract == null || this.user == null || status == null || status.length <= 0)
			return false;

		if (StringUtils.isBlank(this.user.getId()))
			return false;

		List<FlowOfContract2User> fs = this.contract.getFlowInfo();

		if (fs == null || fs.isEmpty())
			return false;

		for (FlowOfContract2User f : fs) {
			// 非活动环节
			if (!Boolean.TRUE.equals(f.getActive()))
				continue;

			// 非指定环节中
			if (!ArrayUtils.contains(status, f.getStatus()))
				continue;

			// 为当前用户
			if (f.getUser() != null && this.user.getId().equals(f.getUser().getId()))
				return true;
		}

		return false;
	}

}
